package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //1. 添加套餐setmeal，保存之前，id是没有值
        this.save(setmealDto);
        //2. 添加套餐菜品关联数据 setmeal_dish
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //TODO 设置每一个SetmealDish的setmealId
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public Page<SetmealDto> pageSetmealDto(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pageInfo = new Page<>(page, pageSize);
        return setmealMapper.pageSetmealDto(pageInfo, name);
    }

    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        //0. 判断套餐是否正在售卖 select count(*) from setmeal where id in (ids) and status=1
        Integer count = this.query().eq("status", 1).in("id", ids).count();
        if (count > 0) {
            //说明有套餐正在售卖，抛出异常
            throw new CustomerException("有套餐正在售卖，不能删除！");
        }
        //1. 删除套餐数据 delete from setmeal where id in (ids)
        this.removeByIds(Arrays.asList(ids));
        //2. 删除套餐菜品数据   delete from setmeal_dish where setmeal_id in (ids)
        setmealDishService.update().in("setmeal_id", ids).remove();
    }

    @Override
    public SetmealDto findById(Long id) {
        return setmealMapper.findById(id);
    }

    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //1. 更新套餐表数据 setmeal
        this.updateById(setmealDto);
        //2. 更新套餐菜品关联数据 setmeal_dish，先删除再添加
        //delete from setmeal_dish where setmeal_id=?
        setmealDishService.update()
                .eq("setmeal_id", setmealDto.getId())
                .remove();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //TODO 给每一个SetmealDish设置setmealId
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishes);
    }
}
